package top.szhome.framework.core.tools.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 图像处理类
 *
 * http://liuyu314.github.io/java/2014/05/24/grayscale/
 * https://cloud.tencent.com/developer/article/1045779
 *
 * @author 唐加利
 * @date 2023/7/23
 **/
public class ImageUtil {

    public static BufferedImage gray(BufferedImage image) {
        return gray(image, 1);
    }

    /**
     * 图片灰度化
     * @param image     待处理图片
     * @param algorithm 处理算法，1-分量法；2-加权法[Y = 0.30R + 0.59G + 0.11B]；3-均值法；4-最值法
     */
    public static BufferedImage gray(BufferedImage image, int algorithm) {
        int width = image.getWidth();
        int height = image.getHeight();

        BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                final int color = image.getRGB(i, j);
                final int r = (color >> 16) & 0xff;
                final int g = (color >> 8) & 0xff;
                final int b = color & 0xff;

                int gray = 0;
                switch (algorithm) {
                    case 2:
                        gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);
                        break;
                    case 3:
                        gray = (NumberUtil.max(r, g, b) + NumberUtil.min(r, g, b) ) / 2;
                        break;
                    case 4:
                        gray = NumberUtil.max(r, g, b);
                        break;
                    case 5:
                        gray = NumberUtil.min(r, g, b);
                        break;
                    case 6:
                        gray = r;
                        break;
                    case 7:
                        gray = g;
                        break;
                    case 8:
                        gray = b;
                        break;
                    case 1:
                    default:
                        gray = (r + g + b) / 3;
                }
                grayImage.setRGB(i, j, argb(255, gray, gray, gray));
            }
        }

        return grayImage;
    }

    /**
     * RGB
     * @param alpha 透明度
     * @param red   红色通道
     * @param green 绿色通道
     * @param blue  蓝色通道
     * @return  返回RGB色彩的整数表示
     */
    public static int argb(int alpha, int red, int green, int blue) {
        int pixel = 0;
        pixel += alpha;
        pixel = pixel << 8;
        pixel += red;
        pixel = pixel << 8;
        pixel += green;
        pixel = pixel << 8;
        pixel += blue;
        return pixel;
    }

    public static void main(String[] args) throws IOException {
        BufferedImage image = ImageIO.read(new File("/Users/qijian/Pictures/壁纸/naruto.jpeg"));
        for(int i = 1; i <= 8;i ++){
            BufferedImage gray = gray(image, i);
            ImageIO.write(gray, "jpeg", new File("/Users/qijian/Pictures/壁纸/naruto-" + i + ".jpeg"));
        }
    }

}
